$(function(){
	requestOld();//获取用户之前的B角
	$("#Part").click(function(){
		$('#myPart').modal('show');
		requestStructucre();
	}); 
	$("#queryUser").change(function(){
		if($(this).val()!=""){
			$(".selctUser").html("");
			queryUser($(this).val());
		}
	});
	$("#submi").click(function(){
		requestSubmit();
	});
	//参与人-向后台请求全部监狱结构信息
function requestStructucre()
{	
	var url="getSuperAndSubordinate.do";//获取部门
	var data={};
	var object={};
	data=YuXi.util.get(url,"null",function(data){
		$(".struc").html("");
		object=data.object;

		for(var i=0;i<object.length;i++)
		{
			var html='<option class="struct-select-option" value="'+object[i].id+'" data-id="'+object[i].id+'">'+object[i].name+'</option>';
			$(".struc").append(html);
		}
		if($("#queryUser").val()!==""){
			$(".selctUser").html("");
			queryUser($("#queryUser").val());
		}
		
			
		
	},function(){});
	
	$("#SearchUserName").click(function(){
		$("#seleStruct").hide();
		$("#searchId").show();
		$("#searchIdCur").show();
		searchUser();
	});
	

	$("#searchIdCur").click(function(){
		$("#seleStruct").show();
		$("#searchId").hide();
		$("#searchIdCur").hide();
	});
	$("#addCho").click(function(){

		var isCheck=[];
		var checkId=[];
		var check=[];
		$("input[name='isCheck']:checked").each(function(){
			
			checkId.push($(this).attr("data-id"));
			isCheck.push($(this).attr("data-na"));	
		}) 
		$("input[name='Check']").each(function(){
			check.push($(this).attr("data-id"));
		});
		for(var i=0;i<isCheck.length;i++){
			var flag=true;
			for(var j=0;j<check.length;j++){
				if(checkId[i]==check[j]){
					flag=false;
					break;
				}
			}
			if(flag==true){
				addUser(isCheck[i],checkId[i]);
			}
		}
		if(isCheck.length!=0){
			clickNum+=1;
		}
		
	
		
		
	})
	$("#removeCho").click(function(){
		 $("input[name='Check']:checked").each(function () {
			$(this).parent().remove();
	          
	     });
	})
	$("#removeAll").click(function(){
		$("#addRequest").find("ul").html("");
		
	});
	$("input[name='isCheck'] :checked").click(function(){
		$("#addRequest").append($(this).parent());
		
		
	});
	
}
function queryUser(id){
	 var structId="structId="+id;
	 var url="getUserListByStruct.do";//获取部门下的角色
	 var data={};
	 var object={}; 
	 data=YuXi.util.get(url,structId,function(data){
		object=data.object;
		for(var i=0;i<object.length;i++)
		{	
			var html= '<li><input type="radio" data-id='+object[i].userId+' data-na='+object[i].userName+' name="isCheck">'+object[i].userName+'</li>';
			$(".selctUser").append(html);
		}
			
	},function(){});
 }
 function addUser(user,id){
	var html= '<li><input type="radio"  data-id='+id+' value='+user+' name="Check">'+user+'</li>';
	$("#addRequest").find("ul").html("");
	$("#addRequest").find("ul").append(html);
}
//参与人选中后的确定按钮
function surePart()
{
	alert();
	 var item=$(".ui-sortable .item");
     $("#node").find(".label1").html("");
     var pearson="";
     for(var i=0;i<item.length;i++)
     {
       firt=item[i].firstChild;
       if(i==item.length-1)
       {
         pearson+=firt.nextSibling.nodeValue;
       }
       else
       {
         pearson+=firt.nextSibling.nodeValue+",";

       }
       html="<span class='parsbox'><span  class='glyphicon glyphicon-user' aria-hidden='true'></span>"+firt.nextSibling.nodeValue+"&nbsp;&nbsp;</span></span>"
       $(".label1").append(html);
     }
     $('#myPart').modal('hide');
	
}
//参与人摸态框，确定按钮
	$("#surePart").click(function(){
		$(".label1").html("");
		var i=0;
		$("input[name='Check']").each(function(){
			i++;
			if(i==0)
			{

			}
			if(i<3)
			{
				var html="<span class='parsbox'><span  class='glyphicon glyphicon-user' aria-hidden='true' ></span><span class='select-data-user' data-id="+$(this).attr("data-id")+">"+$(this).val()+"</span>&nbsp;&nbsp;<span   class='glyphicon glyphicon-remove' aria-hidden='true'></span></span>"
	        	$(".label1").append(html);
			}
			else if(i==3)
			{
				var html="  ...";
				$(".label1").append(html);
			}

	
		})
		$("#myPart").modal('hide');
		removeUser();
		
		
	})
	

var clickNum=0;
function removeUser()
{	
	
	//删除参与人
	$(".glyphicon-remove").click(function(){
		var name=$(this).prev().attr("data-id");
		$("input[name='Check']").each(function(){
			if(name==$(this).attr("data-id")){
				$(this).parent().remove();
			}
		})
		$(this).parent().remove();
		
	});

}

function searchUser(){

	var url="getUserListByAccountOrName.do";//获取部门
	var data={userAccountOrName:$("#accountOrName").val()};
	if($("#accountOrName").val()==""){
			myTool.alert({		
				title:"提示",
				context:"警号或姓名不能为空!",
				isMask:true,
				callback:function(){
					
				}
		 });
		 return false;
	 }
	 
	 YuXi.util.get(url,data,function(data){
		//console.log(data.object);
		var object=data.object;
		if(data.status){
			if(data.object==""){
				myTool.alert({		
					title:"提示",
					context:"请检查警号或姓名是否输入正确!",
					isMask:true,
					callback:function(){
					}
				});
			}else{
				$(".selctUserName").html("");
				for(var i=0;i<object.length;i++)
				{	
					var html= '<li><input type="radio" data-id='+object[i].userId+' data-na='+object[i].userName+' name="isCheck">'+object[i].userName+'</li>';
					$(".selctUserName").append(html);
				}
			}
		}else{
			myTool.alert({		
			title:"提示",
			context:"查询失败,请检查警号或姓名是否输入正确。",
			isMask:true,
			callback:function(){
				}
			});
		}
	},function(){
		
	});
}
//获取用户之前b角
function requestOld()
{
	var url="getCurUserB.do";
	YuXi.util.get(url,null,function(data){
		var object=data.object;
		var html="<span class='parsbox'><span  class='glyphicon glyphicon-user' aria-hidden='true' ></span><span class='select-data-user' data-id="+object.userId+">"+object.userName+"</span>&nbsp;&nbsp;<span   class='glyphicon glyphicon-remove' aria-hidden='true'></span></span>";
		$(".label1").append(html);
	},function(){

	})
}

//确认提交
function requestSubmit()
{
	var url="modifyUserB.do";//提交B角参与人
	var object={};
	object.userName=$(".select-data-user").html();
	
	object.userId=$(".select-data-user").attr("data-id");
	YuXi.util.get(url,object,function(data){
		if(data.status)
		{
			myTool.alert({		
				title:"提示",
				context:"提交成功!",
				isMask:true,
				callback:function(){
					
				}
			})
		}
	},function(){
		myTool.alert({		
				title:"提示",
				context:"提交失败!",
				isMask:true,
				callback:function(){}
			})
		}

	)
}

})
